package com.vf.admin.platform.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.platform.entity.*;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户角色关联Dao
 */
@Repository
public class UserRoleDao extends BaseDao<UserRole> {

    /**
     * 保存或者更新
     */
    public void saveOrUpdate(UserRole userRole){
        if(userRole.getUserid() > 0){
            this.update(userRole);
        }else{
            this.add(userRole);
        }
    }
    /**
     * 为用户分配的角色
     *
     * @return
     */
    public void save(Integer userid,List<String> roleIdList) {
        try {
            ArrayList<Object> paramList = new ArrayList<Object>();
            StringBuilder sbSql = new StringBuilder("insert into bt_user_role (userid,roleid)  values");
            for (String roleid : roleIdList) {
                if (StringEx.isNullOrLing(roleid)) {
                    continue;
                }
                sbSql.append(" (?,?) ,");
                paramList.add(userid);
                paramList.add(roleid);
            }
            String sql = sbSql.substring(0,sbSql.lastIndexOf(","));
            this.update(sql, paramList);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //详情
    public UserRole get(Integer id){
        return this.get(id,UserRole.class);
    }

    //列表查询
    public List<UserRole> findList(Map<String,Object> paraMap){
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from bt_user_role  where 1=1");
        if(StringEx.isNotNull(paraMap.get("userid"))){
            sbStr.append(" and userid = ? ");
            paramList.add(paraMap.get("userid"));
        }
        if(StringEx.isNotNull(paraMap.get("roleid"))){
            sbStr.append(" and roleid = ? ");
            paramList.add(paraMap.get("roleid"));
        }
        String sql = sbStr.toString();
        return this.queryList(sql,paramList,UserRole.class);
    }

    //列表用户拥有的所有的菜单查询
    public List<Menu> findMenuListByUserId(Map<String,Object> paraMap){
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("SELECT * FROM bt_menu m  WHERE m.id IN (SELECT rm.menuid FROM bt_role_menu rm WHERE rm.roleid IN (SELECT ur.roleid FROM bt_user_role ur WHERE 1 = 1  ");
        if(StringEx.isNotNull(paraMap.get("userid"))){
            sbStr.append(" and ur.userid = ? ");
            paramList.add(paraMap.get("userid"));
        }
        sbStr.append(")) order by id desc");

        String sql = sbStr.toString();
        return this.queryList(sql,paramList, Menu.class);
    }

    //列表用户拥有的所有的菜单查询
    public List<RoleMenu> findRoleMenuListByUserId(Map<String,Object> paraMap){
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder(" SELECT rm.menuid,rm.roleid FROM bt_role_menu rm WHERE rm.roleid IN (SELECT ur.roleid FROM bt_user_role ur WHERE 1 = 1 ");
        if(StringEx.isNotNull(paraMap.get("userid"))){
            sbStr.append(" and ur.userid = ? ");
            paramList.add(paraMap.get("userid"));
        }
        sbStr.append(") group by roleid");

        String sql = sbStr.toString();
        List<RoleMenu> roleMenus = this.queryList(sql, paramList, RoleMenu.class);
        return roleMenus;
    }

    /**
     * 根据用户id删除
     */
    public void delByUserId(int userId){
        if (userId <= 0) {
            return;
        }
        String sql = "delete from bt_user_role  where userid = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(userId);
        this.del(sql, paramList);
    }
    /**
     * 根据角色id删除
     */
    public void delByRoleId(int roleId){
        if (roleId <= 0) {
            return;
        }
        String sql = "delete from bt_user_role  where roleid = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(roleId);
        this.del(sql, paramList);
    }

}
